<?php

// +----------------------------------------------------------------------
// | Author: 逯佳 视频详情页面 视频播放页面
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
class SpController extends Controller {

	public $artm;
	public $catm;
	public $rsom;

	public function __construct(){
		parent::__construct();
		$this->artm = M('art');
		$this->catm = M('cat');
		$this->rsom = M('resource');
	}
	//视频评分ajax接口
	public function gradeadd(){
		$artm = $this->artm;
		$id = I('id');
		$sum = I('sum');
		$art = $artm->field('grade,grade_sum')->where(['art_id'=>$id])->find();
		if ($sum > $art['grade']) {
			$data['grade'] = ($sum-$art['grade'])/($art['grade_sum']+1)+$art['grade']; 
		}else if ($sum < $art['grade']) {
			$data['grade'] = $art['grade']-($art['grade']-$sum)/($art['grade_sum']+1); 
		}
		$data['grade_sum'] = $art['grade_sum']+1;
		$art = $this->artm->where(['art_id'=>$id])->save($data);
		$newgrade = substr($data['grade'],0,4);
		$this->ajaxReturn('已有'.$data['grade_sum'].'人评分,新分数为'.$newgrade,'eval');
	} 
	//视频详情页
	public function sp(){
		$id = I('id');
		if (!is_numeric($id)) {
			redirect('/');
		}
		$artm = $this->artm;
		
		$data['art'] = $artm->where(['art_id'=>$id])->find();
		if (empty($data['art'])) {
			redirect('/');
		}else{
			$catm = $this->catm;
			$rsom = $this->rsom;
			//添加浏览量 也就是热门
			$hot['hot'] = $data['art']['hot']+1;
			$artm->where(['art_id'=>$data['art']['art_id']])->save($hot);
			//栏目
			$data['catname'] = $catm->field('catname')->where(['cat_id'=>$data['art']['cat_id']])->find()['catname'];
			//资源
			$data['download'] = $rsom->field('title,thunder,num,r_id')->where(['resource.art_id'=>$data['art']['art_id']])->join('art on resource.art_id = art.art_id ')->order('sum')->select();
			//类
			$data['class'] = $catm->field('catname')->where(['cat_id'=>$data['art']['class']])->find()['catname'];
			//推荐
			$data['similar'] = $artm->field('art_id,title,img')->where(['class'=>$data['art']['class'],'art_id'=>['neq',$data['art']['art_id']]])->limit(8)->select();
			$this->assign($data);
			$this->display();
		}

	}
	public function play(){
		$id = I('id');
		if (!is_numeric($id)) {
			redirect('/');
		}
		$rsom = $this->rsom;
		$data['rso'] = $rsom->field('art.art_id,title,thunder,class,grade,grade_sum')->join('art on art.art_id = resource.art_id')->where(['r_id'=>$id])->find();
		if (empty($data['rso'])) {
			redirect('/');
		}else{
			$catm = $this->catm;
			$data['class'] = $catm->field('catname')->where(['cat_id'=>$data['rso']['class']])->find()['catname'];
			$data['catname'] = $catm->field('catname')->where(['cat_id'=>$data['rso']['cat_id']])->find()['catname'];
			$data['grade'] = substr($data['rso']['grade'],0,-3);
			//设置cookie播放记录 前台js取出
 			$histroy = $_COOKIE['histroy'];
 			$histroy = json_decode($histroy);
			if ($histroy == null) {
				$histroy = [];
			} 
			if (count($histroy) == 5) {
				array_pop($histroy);
			}
			$newhs = [
				'title'=>$data['rso']['title'],
				'art_id'=>$data['rso']['art_id']
			];
			array_unshift($histroy,$newhs);
			setcookie('histroy',json_encode($histroy),time()+3600*24,'/');
			$this->assign($data);
			$this->display();
		}
	}

}